package com.exercise.storage.api;


import com.exercise.commons.po.UserDO;
import com.exercise.commons.vo.Result;
import com.exercise.commons.vo.User;
import com.exercise.storage.apiParam.UserLoginParam;
import com.exercise.storage.apiParam.UserRegisterParam;
import com.exercise.storage.mapper.UserMapper;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserServiceApi {
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/register")
    public Result<User> register(UserRegisterParam userRegisterParam) {
    //public Result<User> register(String userName, String pwd) {
        Result<User> result = new Result<>();

        if (StringUtils.isEmpty(userRegisterParam.getUserName())) {
            result.setCode("600");
            result.setMessage("用户名不能为空");
            return result;
        }
        if (StringUtils.isEmpty(userRegisterParam.getPwd())) {
            result.setCode("601");
            result.setMessage("密码不能为空");
            return result;
        }

        UserDO userDO = userMapper.findByUserName(userRegisterParam.getUserName());
        if (userDO != null) {
            result.setCode("602");
            result.setMessage("用户名已经存在");
            return result;
        }

        // 密码加自定义盐值，确保密码安全
        String saltPwd = userRegisterParam.getPwd() + "_ykd2050";
        // 生成md5值，并转大写字母
        String md5Pwd = DigestUtils.md5Hex(saltPwd).toUpperCase();

        UserDO userDO1 = new UserDO();
        userDO1.setUserName(userRegisterParam.getUserName());
        // 初始昵称等于用户名
        userDO1.setNickName(userRegisterParam.getUserName());
        userDO1.setPwd(md5Pwd);

        userMapper.add(userDO1);

        result.setSuccess(true);

        result.setData(userDO1.toModel());

        return result;
    }

    @RequestMapping("/login")
    public Result<User> login(UserLoginParam userLoginParam) {
    //public Result<User> login(String userName, String pwd) {

        Result<User> result = new Result<>();

        if (StringUtils.isEmpty(userLoginParam.getUserName())) {
            result.setCode("600");
            result.setMessage("用户名不能为空");
            return result;
        }
        if (StringUtils.isEmpty(userLoginParam.getPwd())) {
            result.setCode("601");
            result.setMessage("密码不能为空");
            return result;
        }

        UserDO userDO = userMapper.findByUserName(userLoginParam.getUserName());
        if (userDO == null) {
            result.setCode("602");
            result.setMessage("用户名不存在");
            return result;
        }

        // 密码加自定义盐值，确保密码安全
        String saltPwd = userLoginParam.getPwd() + "_ykd2050";
        // 生成md5值，并转大写字母
        String md5Pwd = DigestUtils.md5Hex(saltPwd).toUpperCase();

        if (!StringUtils.equals(md5Pwd, userDO.getPwd())) {
            result.setCode("603");
            result.setMessage("密码不对");
            return result;
        }

        result.setSuccess(true);

        result.setData(userDO.toModel());

        return result;
    }

    @RequestMapping("/queryUser")
    public List<User> queryUser(List<Long> userIds) {
        if (CollectionUtils.isEmpty(userIds)) {
            return null;
        }
        List<UserDO> userDOS = userMapper.findByIds(userIds);
        List<User> users = new ArrayList<>();
        if (!CollectionUtils.isEmpty(userDOS)) {
            for (UserDO userDO : userDOS) {
                users.add(userDO.toModel());
            }
        }
        return users;
    }

    @RequestMapping("/checkLogin")
    public Boolean checkLogin(HttpServletRequest request) {
        Object userId = request.getSession().getAttribute("userId");
        if (userId == null) {
            return false;
        } else {
            return true;
        }
    }
}
